package com.example.framework;

import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.objects.GroupObject;

public class GroupHelper extends HelperWithWebDriverBase {
	
	private Groups groupModel;
	private int nextGroupId;

	public GroupHelper(ApplicationManager manager) {
		super(manager);
		groupModel = getGroups();
		nextGroupId = Integer.parseInt(manager.getHibernateHelper().getNextGroupId());
	}
	
	public Groups getGroups() {
		if (groupModel == null) {
			groupModel = manager.getHibernateHelper().getGroups();
		}
		return groupModel;
	}
	
	public Groups getUiGroups() {
		  HashSet<GroupObject> set = new HashSet<GroupObject>();
          manager.getNavigationHelper().goToGroupListPage();
          WebElement form = findElements(By.tagName("form")).get(1);
          List<WebElement> checkboxes = form.findElements(By.name("selected[]"));
          for (WebElement checkbox : checkboxes) {
                  String title = checkbox.getAttribute("title");
                  Pattern p = Pattern.compile(".*\\((.*)\\)");
                  Matcher m = p.matcher(title);
                  if(m.matches()) {
                          title = m.group(1);
                  }
                  GroupObject group = new GroupObject()
                                  .setName(title)
                                  .setId(checkbox
                                                  .getAttribute("value"));
                  set.add(group);
          }
          return new Groups(set);
	}
	
	public void createGroup(GroupObject groupObject) {
		manager.getNavigationHelper().goToGroupListPage();
		initGroupCreation();
		fillGroupForm(groupObject);
		submitCreation();
		returnToGroupListPage(); 
		groupObject.setId("" + nextGroupId); 
		groupModel = groupModel.withAdded(groupObject);
		nextGroupId = nextGroupId +1;
	}
	
	public void deleteGroup(GroupObject groupObject) {
		manager.getNavigationHelper().goToGroupListPage();
		checkGroup(groupObject);
		click(By.name("delete"));
		returnToGroupListPage();
		groupModel = groupModel.without(groupObject);
	}
	
	public void modifyGroup(GroupObject oldGroupObject, GroupObject newGroupObject) {
		manager.getNavigationHelper().goToGroupListPage();
		initGroupModification(oldGroupObject);
		fillGroupFormAndReturnToGroupListPage(newGroupObject);
		groupModel = groupModel.without(oldGroupObject).withAdded(newGroupObject);
	}
	
	public void checkGroup(GroupObject groupObject) {
		click(By.xpath("//input[@value='"+groupObject.id+"']"));
	}
	
	public GroupObject getGroup(WebElement checkbox) {
		String title = checkbox.getAttribute("title");
		Pattern p = Pattern.compile(".*\\((.*)\\)");
		Matcher m = p.matcher(title);
		if(m.matches()) {
			title = m.group(1);
		}
			GroupObject group = new GroupObject()
					.setName(title)
					.setId(checkbox
							.getAttribute("value"));
			return group;
	}
	
	private void fillGroupFormAndReturnToGroupListPage(GroupObject groupObject) {
		fillGroupForm(groupObject);
		submitModification();
		returnToGroupListPage();
	}
	
	private void initGroupModification(GroupObject groupObject) {
		checkGroup(groupObject);
		click(By.name("edit"));
	}

	private void initGroupCreation() {
		click(By.name("new"));
	}

	private void fillGroupForm(GroupObject groupObject) {
		enter(By.name("group_name"), groupObject.name);
		enter(By.name("group_header"), groupObject.header);
		enter(By.name("group_footer"), groupObject.footer);
	}

	private void returnToGroupListPage() {
		click(By.linkText("group page"));
	}

}
